package com.example.demo;

import com.google.gson.Gson;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.InetSocketAddress;
import java.net.Socket;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * @author adr
 */
@RestController
public class HelloController {

    @RequestMapping("connectInfo")
    @ResponseBody
    public String connectInfo() {
        return ADBPortRegister.get().info();
    }

    @RequestMapping("uploadFile")
    @ResponseBody
    public Result uploadFile(@RequestParam("fileName") MultipartFile file,
                             @RequestParam(value = "type") String type,
                             @RequestParam(value = "config") String config
    ) {
        SimpleDateFormat format = new SimpleDateFormat("MM-dd HH:mm:ss");
        long start = System.currentTimeMillis();
        String visitTime = format.format(new Date());
        File dir = new File("D://Counting/tempFile/");
        if (!dir.exists()) {
            dir.mkdirs();
        }
        File dest = new File(dir, file.getOriginalFilename());
        try {
            file.transferTo(dest);
        } catch (IOException e) {
            e.printStackTrace();
            return Result.fail(e.getMessage());
        }
        Result result;
        String startTime = format.format(new Date());
        int port = ADBPortRegister.get().providePort();
        if(port == 0){
            return Result.fail("there is no server registered");
        }
        try {
            Socket socket = new Socket();
            socket.connect(new InetSocketAddress("127.0.0.1", port), 2000);
            OutputStream outputStream = socket.getOutputStream();
            WriteHelper.sendData(outputStream, dest, type, config);
            InputStream inputStream = socket.getInputStream();
            String read = ReadHelper.read(inputStream);
            result = new Gson().fromJson(read, Result.class);
        } catch (Exception e) {
            e.printStackTrace();
            result = new Result();
            result.lastError = e.getMessage();
        }
        String endTime = format.format(new Date());
        long t = System.currentTimeMillis() - start;
        long m = (t / 1000) / 60;
        long s = (t / 1000) % 60;
        long ms = t % 1000;
        ADBPortRegister.get().addFreePort(port);
        String cost = m + "m " + s + "s " + ms + "ms";
        System.out.println();
        System.out.println("┌────────────────────────────────");
        System.out.println("│  request start at " + visitTime + ".");
        System.out.println("│  file save to:" + dest.getAbsolutePath() + ".");
        System.out.println("│  connect avd " + port + " at " + startTime + ".");
        System.out.println("│  request end at " + endTime + ", cost " + cost + ".");
        System.out.println("└────────────────────────────────");
        System.out.println();
        return result;
    }
}
